$(function () {
    var potent_datagrid = $("#potent_datagrid");
    var potent_dialog = $("#potent_dialog");
    var potent_form = $("#potent_form");
    var transfer_dialog = $("#transfer_dialog");
    var transfer_form  =$ ("#transfer_form");


    potent_datagrid.datagrid({
        fitColumns: true,
        fit: true,
        toolbar: '#potent_toolbar',
        url: '/client/list.do',
        pagination: true,
        striped: true,
        rownumbers: true,
        singleSelect: true,
        columns: [[
            {title: '创建日期', field: 'inputDate', width: '100'},
            {
                title: '创建人', field: 'input', width: '100', formatter: function (value, row, index) {
                    return value ? value.realname : "<font color='red'>未知</font>";
                }
            },
            {
                title: '负责人', field: 'principal', width: '100', formatter: function (value, row, index) {
                    return value ? value.realname : "<font color='red'>客户池</font>";
                }
            },
            {title: '客户姓名', field: 'name', width: '100'},
            {title: '年龄', field: 'age', width: '100'},
            {
                title: '性别', field: 'gender', width: '100', formatter: function (value, row, index) {
                    var gender = "保密";
                    if (value == 1) {
                        gender = "男";
                    }
                    ;
                    if (value == 2) {
                        gender = "女";
                    }
                    ;
                    return gender
                }
            },
            {
                title: '职业', field: 'job', width: '100', formatter: function (value, row, index) {
                    return value ? value.name : "<font color='red'>未知</font>";
                }
            },
            {
                title: '薪资水平', field: 'pay', width: '100', formatter: function (value, row, index) {
                    return value ? value.name : "<font color='red'>未知</font>";
                }
            },
            {title: '电话', field: 'tel', width: '100'},
            {title: '邮箱', field: 'email', width: '100'},
            {title: '客户地址', field: 'address', width: '100'},
            {
                title: '客户来源', field: 'root', width: '100', formatter: function (value, row, index) {
                    return value ? value.name : "<font color='red'>未知</font>";
                }
            },
            {title: 'QQ', field: 'qq', width: '100'},
            {title: '微信', field: 'weChat', width: '100'},
            {title: '身份证', field: 'card', width: '100'},
            /*{
                title: '状态', field: 'state', width: '100', formatter: function (value, row, index) {
                    return value ? "<font color='green'>在职</font>" : "<font color='red'>离职</font>"
                }
            }*/
        ]]
    })


    potent_dialog.dialog({
        width: 550,
        height: 370,
        buttons: '#potent_buttons',
        closed: true,
        onClose: function () {
            potent_form.form("clear")
        }
    })

    var methodObj = {
        //添加按钮
        add: function () {
            potent_dialog.dialog("open");
            potent_dialog.dialog('setTitle', '新增客户信息');
        },
        //编辑按钮
        edit: function () {
            var data = potent_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            console.log(data.id)
            if (data.job) {
                data["job.id"] = data.job.id;
            }
            if (data.root) {
                data["root.id"] = data.root.id;
            }
            if (data.pay) {
                data["pay.id"] = data.pay.id;
            }
            potent_form.form('load', data);

            potent_dialog.dialog("open");
            potent_dialog.dialog('setTitle', '编辑客户信息');
        },
        //刷新按钮
        reload: function () {
            potent_datagrid.datagrid("reload");
        },
        //取消按钮
        cancel: function () {
            potent_dialog.dialog("close");
        },
        //添加保存按钮
        save: function () {
            potent_form.form('submit', {
                url: '/client/saveOrUpdate.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.cancel();
                            potent_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
        },
        //转移入客户池{
        share: function () {
            var data = potent_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            $.messager.confirm("温馨提示", "你确定要把该用户转移入客户池么吗？", function (r) {
                if (r) {
                    $.get("/client/changePrincipal.do", {id: data.id}, function (data) {
                        if (data.success) {
                            $.messager.alert('温馨提示', '转移成功', 'info', function () {
                                methodObj.cancel();
                                potent_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert('温馨提示', data.msg);
                        }
                    },"json")
                }

            })
        },
        //转移用户{
        transfer: function () {
            var data = potent_datagrid.datagrid('getSelected');
            if (!data) {
                $.messager.alert('温馨提示', "至少选择一条信息");
                return;
            }
            $("#transfer_form").form('load', data);
            $("#name").text(data.name);
            $("#chargeUser").text(data.input.realname);
            transfer_dialog.dialog("open");
        },
        //转移用户取消按钮
        transferCancel: function () {
            transfer_dialog.dialog("close");
        },
        //转移用户保存按钮
        transferSave: function () {
            transfer_form.form('submit', {
                url: '/client/changePrincipal.do',
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert('温馨提示', '保存成功', 'info', function () {
                            methodObj.transferCancel();
                            potent_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert('温馨提示', data.msg);
                    }
                }
            })
            /*$.get("/client/changePrincipal.do", function (data) {
                if (data.success) {
                    $.messager.alert('温馨提示', '转移成功', 'info', function () {
                        methodObj.transferCancel();
                        potent_datagrid.datagrid("reload");
                    });
                } else {
                    $.messager.alert('温馨提示', data.msg);
                }
            },"json")*/
        }
    }

    transfer_dialog.dialog({
        title:'转移客户',
        width: 380,
        height: 380,
        buttons:'#transfer_btns',
        closed: true,
        onClose: function () {
            transfer_form.form("clear")
        }
    })

    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    })

})

